﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReclutamientoCosea
{
	public delegate void CerroVentana(bool cerro);
    public delegate void VentanaLogin(bool exito, bool cancelar);
	
	/// <summary>
	/// Interaction logic for usrLogin.xaml
	/// </summary>
	public partial class usrLogin : UserControl
	{
		//public event CerroVentana SeCerroVentana;
        public event VentanaLogin SeCerroVentana;
        private Persona _personaLogin;

        public Persona PersonaLogin
        {
            get { return _personaLogin; }
            set { _personaLogin = value; }
        }

		public usrLogin()
		{
			this.InitializeComponent();            
            this.Loaded += new RoutedEventHandler(usrLogin_Loaded);
		}

        void usrLogin_Loaded(object sender, RoutedEventArgs e)
        {
            txtUsuario.Focus();
        }

		private void btnAceptar_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            try
            {
                Usuario usr = new Usuario();
                usr.username = txtUsuario.Text.Trim();
                usr.password = txtPassword.Password.Trim();
                PersonaLogin = LogInLogOut.Login(usr);

                if (PersonaLogin.idpersona == "N/A")
                {
                    lblMensaje.Content = "Usuario no encontrado";
//                    txtUsuario.Focus();
                    return;
                }
                if (PersonaLogin.idpersona == "P/I")
                {
                    lblMensaje.Content = "Password Invalido";
                    //txtPassword.Focus();
                    return;
                }

                SeCerroVentana(true,false);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); //Temporal manejo de Log de errores
            }
            finally
            {
                
            }
			
		}

		private void btnCancelar_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			SeCerroVentana(false,true);
		}

		private void txtUsuario_GotFocus(object sender, System.Windows.RoutedEventArgs e)
		{
			lblMensaje.Content = string.Empty ;
		}

		private void txtPassword_GotFocus(object sender, System.Windows.RoutedEventArgs e)
		{
			lblMensaje.Content = string.Empty ;
		}

        private void txtUsuario_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                txtPassword.Focus();
            }
        }

        private void txtPassword_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                btnAceptar_Click(null, null);
            }
        }
	}
}